const queue = []
let isFlushing = false
const resolvedPromise = Promise.resolve()
let currentFlushPromise = null

export function nextTick(fn) {
    const p = currentFlushPromise || resolvedPromise
    return fn ? p.then(fn) : p
}

export function queueJob(job) {
    if (!queue.length || !queue.includes(job)) {
        // 如果队列为空，直接push，，否则判断queue里是否含有该job，没有再添加
        queue.push(job)
        queueFlush()
    }
}

function queueFlush() {
    if (!isFlushing) {
        isFlushing = true
        currentFlushPromise = resolvedPromise.then(flushJobs)
    }
}

function flushJobs() {
    try {
        for (let i = 0; i < queue.length; i++) {
            const job = queue[i]
            job()
        }
    } finally {
        isFlushing = false
        queue.length = 0
        currentFlushPromise = null
    }
}